<?php
namespace app\manage\model;
use think\Db;
use think\Model;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/5
 * Time: 16:25
 */
class TenantManagement extends Model
{
    /**
     * 租客添加显示
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function templateList(){
        $input=input();
        $create_id= create_id($input['token']);
        //经纪人
        $user_id=Db::name('role')
            ->where(['status'=>1])
            ->where(['create_id'=>$create_id])
            ->where(['name'=>'经纪人'])
            ->find();
        $user=Db::name('admin_user')
            ->where(['role_id'=>$user_id['role_id']])
            ->where(['admin_status'=>1])
            ->where(['create_id'=>$create_id])
            ->field('admin_id,name')
            ->select();
        return $user;
    }

    /**
     * 添加的时候判断手机号是否重复
     * @param $phone
     * @return array|false|\PDOStatement|string|Model
     */
    public function tenantPhone($data){
        //经纪人
        $phone=trim( $data['tenant_idcard'] );
        $id=trim( $data['tenant_id'] );
        $create_id= create_id($data['token']);
        $where=[];
        $where['tenant_idcard'] = $phone;
        if($id!=null){
            $where['tenant_id'] = array('neq',$id);
        }

        $user=Db::name('tenant_management')
            ->where(['create_id'=>$create_id])
            ->where($where)
            ->find();
       // var_dump($where,$user);exit;
        return $user;
    }
    /**
     * 添加的时候判断手机号是否重复
     * @param $phone
     * @return array|false|\PDOStatement|string|Model
     */
    public function tenantPhones($phone,$token){
        //经纪人
        $where=[];
        $where['tenant_idcard'] = $phone;
        $create_id= create_id($token);
        $where['create_id'] = $create_id;
        $user=Db::name('tenant_management')
            ->where($where)
            ->find();
        return $user;
    }
    /**
     *租客添加
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function tenantAdd($data){
        //添加操作用户id
        /*$uid=get_user_id($data['token']);
        $arr['te_uid'] = $uid;*/
        $user=Db::name('house_keeper')->where(['tel'=>$data['tenant_tel']])->find();
        if($user!=null){
            $arr['tenant_user_id']=$user['id'];
        }
        //处理数据
        $arr['tenant_name'] = isset($data['tenant_name']) ? trim($data['tenant_name']) : "";
        $arr['tenant_sex'] = tenant_sex(isset($data['tenant_sex']) ? trim($data['tenant_sex']) : "");
        $arr['tenant_tel'] = isset($data['tenant_tel']) ? trim($data['tenant_tel']) : "";
        $arr['tenant_idcard'] = isset($data['tenant_idcard']) ? trim($data['tenant_idcard']) : "";
        $arr['tenant_front'] = isset($data['tenant_front']) ?  base64_image_content("data:image/jpg;base64,".$data['tenant_front']) : "";
        $arr['tenant_famous_race'] = isset($data['tenant_famous_race']) ? trim($data['tenant_famous_race']) : "";
        $model=new \app\manage\model\UserRole();
        $arr['tenant_agent'] =  isset($data['tenant_agent']) ? trim($data['tenant_agent']) : "";
        $arr['create_id'] =   create_id($data['token']);
       // $arr['tenant_agent'] =  isset($data['tenant_agent']) ? trim($data['tenant_agent']) : "";
        $arr['date_of_birth'] = isset($data['date_of_birth']) ? $data['date_of_birth']: "";
        $arr['tenant_back'] = isset($data['tenant_back']) ?  base64_image_content("data:image/jpg;base64,".$data['tenant_back']) : "";
        $arr['tenant_issue'] = isset($data['tenant_issue']) ? $data['tenant_issue']  : "";
        $arr['tenant_address'] = isset($data['tenant_address']) ? $data['tenant_address']  : "";
        $arr['tenant_valid_date'] = isset($data['tenant_valid_date']) ? $data['tenant_valid_date']  : "";
        $arr['tenant_a_time'] = time();
        $data=Db::name('tenant_management')->insertGetId($arr);
        if($data){
            $data= Db::name('tenant_management')->where(['tenant_id'=>$data])->find();
            return $data;
        }else{
            return false;
        }

    }
    /**
     * 租客修改显示
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function tenantOne($id){
        //处理数据
        $data=Db::name('tenant_management')
            ->alias('a')
            ->join('admin_user','a.tenant_agent=admin_user.admin_id')
            ->field('a.*,admin_user.admin_id,admin_user.name')
            ->where(['tenant_id'=>$id])
            ->find();
       // var_dump($data);exit;
        $data['tenant_sex']=tenant_u_sex($data['tenant_sex']);
        return $data;
    }

    /**
     * 修改
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function TenantUpdate($data){
        //处理数据
        $where['tenant_id']=$data['tenant_id'];
        if(strpos($data['tenant_back'],'uploads/') == false){
            $img1=base64_image_content("data:image/jpg;base64,".$data['tenant_back']);
            $arr['tenant_back'] = $img1;
        }
        if(strpos($data['tenant_front'],'uploads/') == false){
            $img2=base64_image_content("data:image/jpg;base64,".$data['tenant_front']);
            $arr['tenant_front'] =$img2 ;
         }

        $user=Db::name('house_keeper')->where(['tel'=>$data['tenant_tel']])->find();
        if($user!=null){
            Db::name('house_keeper')->where(['tel'=>$data['tenant_tel']])->update(['tel'=>$data['tenant_tel']]);
            $arr['tenant_user_id']=$user['id'];
        }
        //处理数据
        $arr['tenant_name'] = isset($data['tenant_name']) ? trim($data['tenant_name']) : "";
        $arr['tenant_sex'] = tenant_sex(isset($data['tenant_sex']) ? trim($data['tenant_sex']) : "");
        $arr['tenant_tel'] = isset($data['tenant_tel']) ? trim($data['tenant_tel']) : "";
        $arr['tenant_idcard'] = isset($data['tenant_idcard']) ? trim($data['tenant_idcard']) : "";
        $arr['tenant_famous_race'] = isset($data['tenant_famous_race']) ? trim($data['tenant_famous_race']) : "";
      //  $arr['tenant_agent'] =  isset($data['tenant_agent']) ? trim($data['tenant_agent']) : "";
        $arr['date_of_birth'] = isset($data['date_of_birth']) ? $data['date_of_birth']: "";
        $arr['tenant_issue'] = isset($data['tenant_issue']) ? $data['tenant_issue']  : "";
        $arr['tenant_address'] = isset($data['tenant_address']) ? $data['tenant_address']  : "";
        $arr['tenant_valid_date'] = isset($data['tenant_valid_date']) ? $data['tenant_valid_date']  : "";
        $arr['tenant_a_time'] = time();
        $data=Db::name('tenant_management')->where($where)->update($arr);
        return $data;
    }



    /**
     * 租客显示
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function index(){
        $data = input();
        $page=isset($data['p']) ? trim($data['p']) : 1;
        $phone=isset($data['phone']) ? trim($data['phone']) : '';
        $create_id=create_id($data['token']);
        //->where('name|code', 'like', '%'.$keyword.'%')
        $where=[];
        if($phone!=null){
            $where['tenant_name|tenant_tel'] = ['like',"%".$phone."%"];
          //  $where['tenant_tel'] = ['like',"%".$phone."%"];
        }
        $where['tenant_state']=1;
        $where['a.create_id']=$create_id;
        $count=Db::name('tenant_management')
            ->join('admin_user','a.tenant_agent=admin_user.admin_id')
            ->alias('a')
            ->where($where)
            ->count();
        $data=Db::name('tenant_management')
            ->alias('a')
            ->join('admin_user','a.tenant_agent=admin_user.admin_id')
            ->field('a.tenant_id,a.tenant_name,a.tenant_sex,a.tenant_tel,a.tenant_idcard,admin_user.name')
            ->where($where)
            ->page($page, 10)
            ->order("tenant_id desc")
            ->select();
        $date['data']=$data;
        $date['count']=$count;
        return $date;
    }


    /**
     * 租客手机号或者身份证搜索租客信息
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function tenantSearch($id,$p){
        $i=input();
        $create_id=create_id($i['token']);
        $where=[];
        $page=isset($p) ? trim($p) : 1;
        if($id!=null){
            $where['tenant_tel|tenant_idcard'] = ['eq',$id];
        //租客查询
            $where['create_id'] = ['eq',$create_id];
        $data=Db::name('tenant_management')
            ->field('tenant_id,tenant_name,tenant_tel,tenant_idcard')
            ->where($where)
            ->find();
        //查询租客和合同关联表
        $user_id=Db::name('tenant_contract')
            ->where(['t_id'=>$data['tenant_id']])
            ->page($page, 10)
            ->order('t_c_id desc')
            ->select();
        $count=Db::name('tenant_contract')
            ->where(['t_id'=>$data['tenant_id']])
            ->count();
        //整理数据
        $user_id_list=array_column($user_id,'t_c_mid');
        $id=implode(',',$user_id_list);

        //查找对应的合同
        $map['c_id'] = array('in',$id);
        $input = input();
        $map['create_id'] =  create_id($input['token']);
        $date=Db::name('contract_management')
            ->field('c_id,c_number,c_a_time,c_d_time,c_room,c_state')
            ->where($map)
            ->select();
        //根据合同查找合同住房
        foreach($date as $k=>$v){
            $date[$k]['housing']=Db::name('contracted_housing')
                ->field('c_number')
                ->where(['c_c_mid'=>$v['c_id']])
                ->select();
        }
        $data['contract']=$date;
        $data['count']=$count;
        return $data;
     }
        return $where;
    }

    /**
     * 根据电话号码找到租客
     * @param $tel 电话号码
     * @return array|false|\PDOStatement|string|Model
     */
     public function phone($tel){
         $user=Db::name('tenant_management')->where(['tenant_tel'=>$tel])->find();
         return $user;
     }

    /**
     * 根据小程序用户id找到租客
     * @param $tenant_user_id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
     public function detailKeeper($tenant_user_id) {
         $user=Db::name('tenant_management')->where(['tenant_user_id'=>$tenant_user_id])->find();
         return $user;
     }

    /**
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function m_update($id, $data) {
        $data = Db::name('tenant_management')
            ->where(['tenant_id'=>$id])
            ->update($data);
        return $data;
    }

    /**
     * @param $id
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail($id) {
        $user=Db::name('tenant_management')->where(['tenant_user_id'=>$id])->find();
        return $user;
    }

    /**
     * 用户显示
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function tenantUser(){
        $data = input();
        $number=isset($data['number']) ? trim($data['number']) : 10;
        $page=isset($data['p']) ? trim($data['p']) : 1;
        $phone=isset($data['phone']) ? trim($data['phone']) : '';
        $sex=isset($data['sex']) ? trim($data['sex']) : '';
        $status=isset($data['status']) ? trim($data['status']) : '';
        $tenant=isset($data['tenant']) ? trim($data['tenant']) : '';
        $where=[];
        if($phone!=null){
            $where['a.tel|a.name'] = ['like',"%".$phone."%"];
        }
        if($status!=null){
            $where['a.status'] = ['eq',$status];
        }
        if($sex!=null){
            $where['a.sex'] = ['eq',$sex];
        }
        if($tenant==1){
            $where['b.tenant_user_id'] = ['neq',''];
        }elseif($tenant==2){
            $where['b.tenant_user_id'] = null;
        }
        $count=Db::name('house_keeper')
            ->alias('a')
            ->join('tenant_management b','b.tenant_user_id=a.id','left')
            ->where($where)
            ->count();
        $data=Db::name('house_keeper')
            ->alias('a')
            ->join('tenant_management b','b.tenant_user_id=a.id','left')
            ->field('a.id,a.status,a.tel,a.name,a.sex,b.tenant_name,b.tenant_idcard')
            ->where($where)
            ->page($page, $number)
            ->order("id desc")
            ->select();
        $date['data']=$data;
        $date['count']=$count;
        return $date;
    }
    /**
     * 用户禁用和解除
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function userStatus($data){
        $count[]='';
        $id=trim( $data['id'] );
        $type=trim( $data['type'] );
        $date=Db::name('house_keeper')->where(['id'=>$id])->update(['status'=>$type]);
        return $date;
    }

}